package com.codeying.servlet;

import com.codeying.dao.*;
import com.codeying.entity.*;
import com.codeying.utils.MyUtils;
import com.codeying.utils.component.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 起始页
 */
@WebServlet("/welcome")
public class IndexServlet extends AbsServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        req.setAttribute("sysName","汽车租赁系统");
        LoginUser user = MyUtils.getUser(req);
        String action = req.getParameter("action");//操作实体
        String entityName = req.getParameter("entityName");//操作实体
        String id = req.getParameter("id");
        //默认实体
        if(MyUtils.isEmpty(entityName)){
            entityName = "car";
        }
        req.setAttribute("entityName",entityName);

        //我的收藏数
        if (user == null) {
            req.setAttribute("myStartCount",0);
        } else {
            int starCount = StarDao.me.count(new QueryMap().add("userid", String.valueOf(user.getId())));
            req.setAttribute("myStartCount",starCount);
        }

        if(MyUtils.isEmpty(action)){
            //汽车首页和详情页
            if(entityName.equals("car")){
                //热门
                List<Star> hotList = StarDao.me.topN("car",4);
                req.setAttribute("hotList",hotList);
                if(MyUtils.isEmpty(id)){//首页
                    //查询条件
                    String title = req.getParameter("title");
                    if(MyUtils.isNotEmpty(title)){
                        req.setAttribute("title",title);
                    }
                    //公告
                    List<Notice> notices = noticeService.list();
                    req.setAttribute("notices",notices);
                    //数据
                    List<Car> itemList = carService.list(new QueryMap().add("showtitle",title));
                    req.setAttribute("itemList",itemList);
                    req.getRequestDispatcher("/web/index.jsp").forward(req, resp);
                }else {
                    //详情页
                    Car detail = carService.getById(id);
                    req.setAttribute("detail",detail);
                    //收藏数
                    int starCount = StarDao.me.count(new QueryMap().add("itemid", String.valueOf(detail.getId())));
                    req.setAttribute("starCount",starCount);
                    req.getRequestDispatcher("/web/car-detail.jsp").forward(req, resp);
                }
            }
        }
        //是否收藏
        else if(action.equals("hasStar")){
            resp.setHeader("Content-type", "text/html;charset=UTF-8");
            resp.setContentType("application/json; charset=utf-8");
            resp.setCharacterEncoding("UTF-8");
            if(user!=null){
                String starId = (String) StarDao.me.hasStar(String.valueOf(user.getId()),user.getRole(),id,entityName);
                resp.getWriter().print(ApiResult.successData(starId));//返回收藏id
            }else {
                resp.getWriter().print(ApiResult.successData(null));//标记成功
            }
        }
        //收藏列表
        else if(action.equals("stars")){
            if(user==null){
                req.setAttribute("message","进入我的收藏之前请先登录！");
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            }
            List<Star> stars = StarDao.me.userStar(String.valueOf(user.getId()),user.getRole());
            req.setAttribute("entityName","star");
            req.setAttribute("stars",stars);
            req.getRequestDispatcher("/web/stars.jsp").forward(req, resp);
        }
        else {
            //判断是否登录
            if (user == null) {
                resp.setHeader("Content-type", "text/html;charset=UTF-8");
                resp.setContentType("application/json; charset=utf-8");
                resp.setCharacterEncoding("UTF-8");
                resp.getWriter().print(ApiResult.fail("请先登录！"));
                return;
            }
            //收藏
            if(action.equals("star")){
                resp.setHeader("Content-type", "text/html;charset=UTF-8");
                resp.setContentType("application/json; charset=utf-8");
                resp.setCharacterEncoding("UTF-8");
                boolean res = StarDao.me.star(String.valueOf(user.getId()),user.getRole(),id,entityName);
                if(res){
                    resp.getWriter().print(ApiResult.successMsg("收藏成功"));//标记成功
                }else {
                    resp.getWriter().print(ApiResult.fail("已经收藏过了！"));
                }
            }
            //取消收藏
            else if(action.equals("cancelStar")){
                resp.setHeader("Content-type", "text/html;charset=UTF-8");
                resp.setContentType("application/json; charset=utf-8");
                resp.setCharacterEncoding("UTF-8");
                StarDao.me.deleteById(id);
                resp.getWriter().print(ApiResult.success());//标记成功
            }

        }
    }

}


